package cn.lg.soar.system.auth.client;

import cn.lg.soar.common.util.BeanCopier;
import cn.lg.soar.core.annotations.ApiClient;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.model.PermitDTO;
import cn.lg.soar.system.api.service.IPermitApi;
import cn.lg.soar.system.auth.entity.Permit;
import cn.lg.soar.system.auth.service.IPermitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author luguoxiang
 * @date 2022/1/3
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ApiIgnore
@ApiClient("boot")
@RestController
@RequestMapping(ModuleConst.CLIENT_PATH + "/permit")
public class PermitApiClient implements IPermitApi {

    @Autowired
    private IPermitService service;

    private final static BeanCopier<Permit, PermitDTO> ENTITY_TO_DTO = BeanCopier.create(Permit.class, PermitDTO.class);

    @GetMapping("codesByRole")
    @Override
    public List<String> codesByRoleId(@RequestParam Long roleId) {
        return service.codesByRoleId(roleId);
    }

    @GetMapping("listByCodes")
    @Override
    public List<PermitDTO> listByCodes(@RequestParam List<String> codes) {
        if (CollectionUtils.isEmpty(codes)) {
            return Collections.emptyList();
        }
        return ENTITY_TO_DTO.convertBatch(service.listByCodes(codes)).collect(Collectors.toList());
    }

    @GetMapping("listByIds")
    @Override
    public List<PermitDTO> listByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return ENTITY_TO_DTO.convertBatch(service.listByIds(ids)).collect(Collectors.toList());
    }

    @GetMapping("getById")
    @Override
    public PermitDTO getById(@RequestParam Long id) {
        return ENTITY_TO_DTO.convertNullable(service.getById(id));
    }
}
